package com.threefivework.mymall.model.domain;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

/**
 * @author yuanshi.fu
 * @description: 处理JAVA对象转换成XML时添加<!CDATA[ ]]>标签
 * @date 2018/6/26 下午1:54
 */
public class CDATAConvert implements Converter {
    /**
     * java对象转换为xml
     */
    public void marshal(Object object, HierarchicalStreamWriter writer,
                        MarshallingContext context) {

        String prefix = "<![CDATA[";
        String suffix = "]]>";
        String trans = prefix + object + suffix;
        writer.setValue(trans);
    }

    /**
     * xml转换成JAVA对象
     */
    public Object unmarshal(HierarchicalStreamReader reader,
                            UnmarshallingContext context) {

        return reader.getValue();
    }

    /**
     * 判断字段是否是需要转换的类型
     */
    public boolean canConvert(Class paramClass) {
        return String.class.isAssignableFrom(paramClass);
    }
}
